package com.acwing.partition20;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/1/18 21:12
 */
public class AC1922懒惰的牛 {

    private static final int N = 1000010;
    private static int[] nums = new int[N], sum = new int[N];
    private static int n = 0, k = 0, max = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        k = Integer.parseInt(s[1]);
        for (int i = 0; i < n; i++) {
            s = reader.readLine().split("\\s+");
            int x = Integer.parseInt(s[1]), w = Integer.parseInt(s[0]);
            nums[x] = w;
            max = Math.max(max, x);
        }
        writer.write(resolve() + "\n");
        writer.flush();
    }

    private static int resolve() {
        int answer = 0;
        //计算前缀和
        for (int i = 1; i <= max + 1; i++) sum[i] = sum[i - 1] + nums[i - 1];
        //遍历每个坐标i，计算[i-k]~[i+k]的值，保留最大
        for (int i = 0; i <= max; i++) {
            int a = Math.max(i - k, 1), b = Math.min(max + 1, i + k);
            answer = Math.max(answer, sum[b] - sum[a - 1]);
        }
        return answer;
    }
}
